package com.faithbj.printer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

/**
 * 模拟登录人人网
 * 人人网登录用户名密码验证通过之后会给浏览器发送一个重定向
 * @author yangtongjiang
 *
 */
public class LoginRenren {
	private final String renRenLoginURL = "http://10.10.0.20/prpall/UICentralControl?SelfPage=/common/pub/UILogonInput.jsp";
//	private String loginFailureURL = "http://www.renren.com/SysHome.do?catchaCount=2&failCode=4";
	private final String loginSuccessURL = "http://10.10.0.20/prpall/processCodeInput.do?actionType=getComCodeOptionsText&userCode=07190007";
    
    /**
     * 登录验证，验证成功(未发生连接异常等，用户名密码不一定验证通过)返回要重定向的url，否则返回空字符串
     * @param username
     * @param passwd
     * @return
     */
    private String loginCheck(CloseableHttpClient httpclient, String username, String passwd){
    	String redirectUrl = "";
    	HttpPost httpost = new HttpPost(this.renRenLoginURL);
    	 List<NameValuePair> nvps = new ArrayList<NameValuePair>();
         //设置登录后跳转到的页面
//         nvps.add(new BasicNameValuePair("origURL", redirectURL));
//         nvps.add(new BasicNameValuePair("domain", "renren.com"));
//         nvps.add(new BasicNameValuePair("isplogin", "true"));
//         nvps.add(new BasicNameValuePair("formName", ""));
//         nvps.add(new BasicNameValuePair("method", ""));
//         nvps.add(new BasicNameValuePair("submit", "登录"));
//         nvps.add(new BasicNameValuePair("email", username));
//         nvps.add(new BasicNameValuePair("password", passwd));
         nvps.add(new BasicNameValuePair("isLogon", "Y"));
         nvps.add(new BasicNameValuePair("IEVersion", "6"));
         nvps.add(new BasicNameValuePair("UserCode", "07190007"));
         nvps.add(new BasicNameValuePair("Password", "Zz111111"));
         nvps.add(new BasicNameValuePair("ClassCode", null));
         nvps.add(new BasicNameValuePair("ClassCodeSelect", "05"));
         nvps.add(new BasicNameValuePair("ClientInfo", null));
         nvps.add(new BasicNameValuePair("ComCode", "07000000"));
         nvps.add(new BasicNameValuePair("IEVersion", "IE 8"));
         nvps.add(new BasicNameValuePair("image.x", "12"));
         nvps.add(new BasicNameValuePair("image.y", "9"));
         nvps.add(new BasicNameValuePair("image.y", "9"));
         nvps.add(new BasicNameValuePair("RiskCodeSelect", "0509"));
         nvps.add(new BasicNameValuePair("RiskCode", "0509"));
         try {
             httpost.setEntity(new UrlEncodedFormEntity(nvps));
             HttpResponse response = httpclient.execute(httpost);
             redirectUrl = response.getFirstHeader("Location").getValue();
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             httpost.abort();
         }
         return redirectUrl;
    }
    
    public void login(CloseableHttpClient httpclient, String username, String passwd) throws ClientProtocolException, IOException{
    	String redirectUrl = this.loginCheck(httpclient, username, passwd);
    	System.out.println("redirectUrl="+redirectUrl);
    	System.out.println("*************************************");
    	if(!redirectUrl.equals(this.loginSuccessURL)){
    		System.out.println("登录失败！");
    		return;
    	}
    	HttpGet httpget = new HttpGet(redirectUrl);
    	ResponseHandler<String> responseHandler = new BasicResponseHandler();
    	String html = httpclient.execute(httpget, responseHandler);
    	System.out.println(html);
    }
    
    public static void main(String[] args){
    	
    	List<Header> headList = new ArrayList<Header>();
        headList.add(new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));
    	
    	String userName = "";
        String password = "";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        LoginRenren renren = new LoginRenren();
        try {
			renren.login(httpclient, userName, password);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				httpclient.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }

}
